package com.hsc.account;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.hsc.db.tools.AccountList;
import com.hsc.db.tools.AccountService;
import com.hsc.db.tools.EmailService;
import com.hsc.db.tools.ItemList;
import com.hsc.db.tools.ItemService;
import com.hsc.db.tools.UseItemService;
import com.hsc.db.tools.useitem;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class addtype extends Dialog {

	public addtype(Context context) {
		super(context);
	}

	public addtype(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private GridView typeview;
		private int iyear;
		private int imonth;
		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;
		private  EditText  txtType;
		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 *
		 * @param title
		 * @return
		 */

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 *
		 * @param positiveButtonText
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;

			return this;
		}

		public Builder setPositiveButton(String positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}
		public addtype create(Context context,GridView typeview,final UseItemService typeService,int year,int month) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final addtype dialog = new addtype(context,R.style.Dialog);
			View layout = inflater.inflate(R.layout.addtype, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			addtype.Builder.this.imonth = month;
			addtype.Builder.this.iyear = year;
			addtype.Builder.this.typeview=typeview;

			txtType = (EditText)layout.findViewById(R.id.edtType);



			txtType.requestFocus();

			openInputMethod(txtType);

			((Button) layout.findViewById(R.id.btnQuxiao))
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							dialog.dismiss();


						}
					});




			((Button) layout.findViewById(R.id.btnQueren))
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {

							String type =txtType.getText().toString().trim();

							if(type.length()>4)
							{
								Toast.makeText(addtype.Builder.this.context, "分组名应小于4个字", Toast.LENGTH_SHORT).show();
								return;
							}



							useitem item = new useitem();
							item.setFlag("1");
							item.setUsetype(type);
							typeService.save(item);

							ArrayList<useitem> arr = new ArrayList<useitem>();
							arr.add(item);

							ItemService items = new ItemService(addtype.Builder.this.context);
							sendTypeThread sd = new sendTypeThread(arr, items);
							sd.start();

							//加载数据
							List<Map<String, Object>> data = typeService.getData(iyear, imonth);
							gridadapter2 sim_adapter = new gridadapter2(addtype.Builder.this.context, data,"-1");
							addtype.Builder.this.typeview.setAdapter(sim_adapter);

							dialog.dismiss();
						}
					});



			dialog.setContentView(layout);


			return dialog;
		}

		public void openInputMethod(final EditText editText) {

			Timer timer = new Timer();

			timer.schedule(new TimerTask() {

				public void run() {

					InputMethodManager inputManager = (InputMethodManager) editText

							.getContext().getSystemService(

									Context.INPUT_METHOD_SERVICE);

					inputManager.showSoftInput(editText, 0);

				}

			}, 200);
		}

	}
}